//
//  ViewController.m
//  TFY_PlayerView
//
//  Created by 田风有 on 2019/6/20.
//  Copyright © 2019 田风有. All rights reserved.
//

#import "ViewController.h"
#import "ViewTableViewCell.h"

#import "TFY_PlayerHeader.h"

#import "PlayerModel.h"
#import "PlayerTwoController.h"
@interface ViewController ()

@property(nonatomic , strong)NSMutableArray *datSouce;

@property(nonatomic , strong)PlayerModel *playerModel;

@property(nonatomic , assign)NSInteger page;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"视频播放";
    self.navigationController.tfy_titleColor = [UIColor redColor];
    self.navigationController.tfy_titleFont = [UIFont systemFontOfSize:15];
    
    self.page = 1;
    
    [self addharder];
    
    [self addfooter];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView tfy_AutoSize:0 top:0 right:0 bottom:0];
}

-(void)loadNewData{
    [self dataSoucepage:self.page];
}

-(void)loadfooter{
    self.page ++;
    [self dataSoucepage:self.page];
}

-(void)tableViewLayout{
    [self.tableView tfy_tableViewMaker:^(TFY_TableViewMaker * _Nonnull tableMaker) {
        [tableMaker tfy_addSectionMaker:^(TFY_SectionMaker * _Nonnull sectionMaker) {
            
            [sectionMaker.tfy_dataArr(TFY_DataArr(self.datSouce)) tfy_cellMaker:^(TFY_CellMaker * _Nonnull cellMaker) {
                
                cellMaker.tfy_cellClass(TFY_CellClass(ViewTableViewCell))
                .tfy_adapter(^(__kindof ViewTableViewCell *cell, id data, NSIndexPath *indexPath){
                    
                    cell.models = data;
                })
                .tfy_event(^(__kindof UITableView *tableView,NSIndexPath *indexPath,id data){
                
                    Data *models = data;
                    PlayerTwoController *vc = [PlayerTwoController new];
                    vc.models = models;
                    [self.navigationController pushViewController:vc animated:YES];
                })
                .tfy_rowHeight(60);
            }];
        }];
    }];
}

-(void)dataSoucepage:(NSInteger)page{
    PlayerCommand *command = [PlayerCommand new];
    command.type = 2;
    command.page = page;
    [[command.picCommand execute:@1] subscribeNext:^(id  _Nullable x) {
        if ([x isKindOfClass:[PlayerModel class]]) {
            self.playerModel = x;
            if (self.page==1) {
                
                self.datSouce = [NSMutableArray arrayWithArray:self.playerModel.data];
                
                [self.tableView.mj_header endRefreshing];
            }
            else{
                
                [self.datSouce addObjectsFromArray:self.playerModel.data];
                
                [self setNoMoreData:NO];
            }
            
            [self tableViewLayout];
        }
        [self.tableView reloadData];
    }];
}

-(NSMutableArray *)datSouce{
    if (!_datSouce) {
        _datSouce = [NSMutableArray array];
    }
    return _datSouce;
}

@end
